import { reactive, readonly, ref } from 'vue';

import { useStore, Store } from './Store';

type Config = { isCollapse: boolean };
class ConfigStore extends Store<Config> {
  /**
   *
   */
  constructor() {
    super('config');
  }
}
class TokenStore extends Store<Token> {
  /**
   *
   */
  constructor() {
    super('TokenStore');
  }
}
class UserStore extends Store<User> {
  constructor() {
    super('user');
  }

  changeName(name: string) {
    this.state.name = name;
  }
  updateMenus(menus: Menu[]) {
    this.state.menus = menus;
    super.saveChanges();
  }
  create(user: User) {
    this._state = user;
  }
}

export const store = reactive({
  user: new UserStore(),
  token: new TokenStore(),
  config: new ConfigStore(),
});
export const toxicities = ['微毒', '低毒', '中毒', '高毒', '剧毒'];
export const categories = [
  '杀菌剂',
  '杀虫剂',
  '除草剂',
  '杀螨剂',
  '杀鼠剂',
  '植物调节剂',
  '卫生杀虫剂',
];
export const productionTypes = ['农药登记证持有人生产', '委托加工', '委托分装'];
export const pesticideRegistrationType = ['PD', 'WP', '临时登记证'];
export const dosageForms = [
  '原药',
  '母药',
  '乳油',
  '悬浮剂',
  '干悬浮剂',
  '水乳剂',
  '微乳剂',
  '浓乳剂',
  '微胶囊悬浮剂',
  '可溶性粉剂',
  '可湿性粉剂',
  '水分散性粒剂',
  '油剂',
  '超低量油剂',
  '粉剂',
  '颗粒剂',
  '拌种剂',
  '种衣剂',
  '烟剂',
  '毒饵',
  '片剂',
  '气雾剂',
  '蚊香',
  '撒滴剂',
  '泡腾片剂',
  '缓释剂',
  '结晶粉',
  '其它',
  '可溶液剂',
];
// export function createStore<T>(obj?: (new (...args: any[]) => T) | T) {
//   if (!obj) {
//     return {} as T;
//   }

//   if (typeof obj instanceof obj) {
//     return new obj();
//   }
//   return obj;
// }
function useStore2<T extends object>(obj?: T) {
  obj ??= {} as T;
  console.error('new Proxy');

  return new Proxy(obj, {
    get(target: T, key: string | symbol) {
      let value = Reflect.get(target, key);
      if (!value && !/[__|Symbol]/.test(key.toString())) {
        console.error(target, key);
      }
      console.log(target, key.toString(), typeof key);
      return Reflect.get(target, key);
    },
    set(target: T, key: string, value: T) {
      console.error(key, value);
      return Reflect.set(target, key, value);
    },
  });
}

// export const store2 = reactive({
//   user: useStore2<Readonly<UserInstance>>(uu),
// });

//export const store3 = reactive(useStore2<Readonly<typeof aa>>(aa));
